const canvas = require("canvas"); //npm install canvas
const echarts = require("echarts"); //npm install echarts
const fs = require('fs'); //node内置模块,直接引入
const path = require('path')

//创建一个canvas实例
let ctx = canvas.createCanvas(600, 400)
//将canvas实例设置为echarts容器
echarts.setCanvasCreator(() => ctx)
//使用canvas实例为容器创建echarts实例
let chart = echarts.init(ctx)
//echarts配置项
let date1 = ['A', 'B', 'C', 'D', 'E', 'F']
let data1 = [5, 20, 36, 10, 10, 20]
var date_now = new Date()

let option = {
    title: {
        text: 'download speed' + date_now
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: date1
    },
    yAxis: {
        min: 1,
        type: "value",
        axisLabel: {
            show: true
        },
        splitNumber: 15
    },
    series: [{
        name: 'speed',
        type: 'bar',
        data: data1
    }]
}
//设置图标实例配置项
chart.setOption(option);
//保存图片
const imgPath = path.resolve(__dirname, "test" + '.png')
fs.writeFileSync(imgPath, chart.getDom().toBuffer());
